﻿Namespace LightSwitchApplication

    Public Class MainAssetsTable

        Private Sub MainAssetsTable_Created()
            DateCreated = Now
            Createdby = Application.User.FullName
            LastModified = Now
            LastModifiedBy = Application.User.FullName
        End Sub

        Private Sub AccumulatedDepreciaton_Compute(ByRef result As Decimal)
            Dim depreciationYears As Integer = 0
            Dim disposeddateSold As Date = Nothing

            If Category Is Nothing Then
                depreciationYears = 0
            ElseIf Category.Years Is Nothing Then
                depreciationYears = 0
            Else
                depreciationYears = Category.Years
            End If

            If DisposedAsset Is Nothing Then
                disposeddateSold = Now
            ElseIf DisposedAsset.DateSold Is Nothing Then
                disposeddateSold = Now
            Else
                disposeddateSold = DisposedAsset.DateSold
            End If

            Dim origAssetAmount As Integer = 0
            If AssetAmount Is Nothing Then
                origAssetAmount = 0
            Else
                origAssetAmount = AssetAmount
            End If

            Dim period As Integer = If(Not DateAcquired Is Nothing _
                                                              , DateDiff(DateInterval.Year, CDate(DateAcquired), Now) _
                                                              , 0)

            If Not period > depreciationYears Or depreciationYears = 0 Then


                Select Case Category.DepreciationMethod
                    Case 1 'straight line
                        result = If(Not DateAcquired Is Nothing, _
                       If(depreciationYears > 0 _
                          , ( _
                              ( _
                                  If(origAssetAmount > 0 _
                                     , origAssetAmount - If(SalvageValue > 0, SalvageValue, 0) _
                                     , If(BaseCost Is Nothing, 0, BaseCost) + If(VAT Is Nothing, 0, VAT)) / depreciationYears) / 12) * DateDiff(DateInterval.Month, CDate(DateAcquired), CDate(If(Not disposeddateSold = Nothing _
                                                                                                                                                                                                        , disposeddateSold _
                                                                                                                                                                                                        , Now) _
                                                                                                                                                                                                    ) _
                                                                                                                                                                                                ) _
                         , 0) _
                     , 0)
                    Case 2 'sum of years
                        Dim depreciableCost As Decimal = If(origAssetAmount > 0 _
                                     , origAssetAmount - If(SalvageValue Is Nothing, 0, SalvageValue) _
                                     , 0)

                        If Not depreciableCost = 0 Or Not depreciationYears = 0 Then
                            For i = 1 To period
                                result += ((depreciableCost) * (depreciationYears - i + 1) * 2) / ((depreciationYears) * (depreciationYears + 1))
                            Next i
                        End If
                    Case 3 'double declining balance
                        Dim depreciationRate As Decimal = If(depreciationYears > 0, (1 / depreciationYears) * 2, 0)
                        If Not depreciationRate = 0 Or Not depreciationYears = 0 Then
                            For i = 1 To period
                                result += (origAssetAmount - result) * depreciationRate
                            Next i
                        End If
                    Case Else
                        result = 0
                End Select
                ' Set result to the desired field value
            Else
                result = origAssetAmount
            End If


        End Sub

        Private Sub TotalCost_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = If(AssetAmount > 0, AssetAmount, If(BaseCost Is Nothing, 0, BaseCost) + If(VAT Is Nothing, 0, VAT))

        End Sub

        Private Sub Assetno_Validate(results As EntityValidationResultsBuilder)
            ' results.AddPropertyError("<Error-Message>")


        End Sub

        Private Sub AssetDisplayForDDL_Compute(ByRef result As String)
            ' Set result to the desired field value
            If Me.DescriptiveName IsNot Nothing Then
                result = Assetno & " (" & DescriptiveName & ")"
            Else
                result = Assetno
            End If
        End Sub

        Private Sub AssignedToDisplay_Compute(ByRef result As String)
            ' Set result to the desired field value
            result = Person.FullName
        End Sub
    End Class

End Namespace
